(function(global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('hy5b/t/frontend')) :
    typeof define === 'function' && define.amd ? define(['frontend'], factory) :
    global.dataTable = factory(t);
}(this, function(t) {
  'use strict';

  var T = t || {}

  var CTable = function() {
    this.ahead_ = [];
    this.acolum_ = [];
    this.fline_ = null;
  };

  CTable.prototype.setHead = function(ahead) {
    this.ahead_ = ahead;
    return this;
  }

  CTable.prototype.setColumn = function(acolumn, fline) {
    this.acolum_ = acolumn;
    this.fline_ = fline;
    return this;
  }

  CTable.prototype.render = function(adataSet, fcallback) {
    var ahead = null,
      acolumn = null,
      atable = null,
      fline = null,
      shtml = "",
      shead = "",
      sbody = "";

    ahead = this.ahead_;
    acolumn = this.acolum_;
    atable = adataSet;
    fline = this.fline_;

    if (!ahead) {
      for (var i = 0; i < acolumn.length; i++) {
        if (typeof(acolumn[i]) == "string") {
          ahead[i] = acolumn[i];
        } else if (typeof(acolumn[i]) == "object") {
          ahead[i] = acolumn[i].key;
        }
      }
    }

    shtml = "<table class='table table-bordered' cellspacing='0' cellpadding='0'>";

    shead = "<tr>";
    for (var i = 0; i < ahead.length; i++) {
      shead += "<th>" + ahead[i] + "</th>";
    }
    shead += "</tr>";
    shtml += shead;

    sbody = "";
    for (var i = 0; i < atable.length; i++) {
      if (fline)
        sbody += "<tr class='" + fline(i, atable) + "'>";
      else if (i % 2 == 0)
        sbody += "<tr class='table-even'>";
      else
        sbody += "<tr class='table-odd'>";

      for (var j = 0; j < acolumn.length; j++) {
        var param;
        if (typeof(acolumn[j]) == "string" && acolumn[j] != "") {
          param = acolumn[j];
          var val = atable[i][param];
          val = (val === "" ? "-" : T.escapeHtml(val));
          sbody += "<td>" + val + "</td>";
        } else if (T.isFunction(acolumn[j])) {
          sbody += "<td>" + acolumn[j](i, atable) + "</td>";
        } else if (T.isObject(acolumn[j])) {
          param = acolumn[j].key;

          if (acolumn[j].enableHtml)
            sbody += "<td>" + atable[i][param] + "</td>";

          else if (acolumn[j].format) {
            if (param == undefined)
              sbody += "<td>" + acolumn[j].format(i, atable) + "</td>";
            else
              sbody += "<td>" + acolumn[j].format(atable[i][param], i, atable) + "</td>";
          } else if (acolumn[j].mformat) {
            // oret {span:"", total:0, cur:0, val:""}
            var oret = acolumn[j].mformat(atable[i][param], i, atable);
            if (oret.firstFlag)
              sbody += "<td " + oret.span + "span=\"" + oret.total + "\">" + oret.val + "</td>";
          } else
            sbody += "<td>-</td>";
        } else
          sbody += "<td>-</td>";
      }
      sbody += "</tr>";
    }
    shtml += sbody;
    shtml += "</table>";

    if (fcallback)
      fcallback(sbody, shtml);
    else
      return shtml;
  }
  return CTable

}))
